﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SharpSoft.Data
{
    using System.Reflection;
    /// <summary>
    /// 模型描述
    /// </summary>
    public class ModelDescriptor
    {
        public ModelDescriptor(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            this.ModelType = type;

            var mpa = type.GetCustomAttribute<MappingNameAttribute>();
            if (mpa != null)
            {
                this.MappingName = mpa.Name;
            }
            var dna = type.GetCustomAttribute<System.ComponentModel.DisplayNameAttribute>();
            if (dna != null)
            {
                DisplayName = dna.DisplayName;
            }

            var da = type.GetCustomAttribute<System.ComponentModel.DescriptionAttribute>();
            if (da != null)
            {
                Description = da.Description;
            }
        }
        /// <summary>
        /// 模型类型
        /// </summary>
        public Type ModelType { get; private set; }
        /// <summary>
        /// 映射名称
        /// </summary>
        public string MappingName { get; set; }

        /// <summary>
        /// 显示名称
        /// </summary>
        public string DisplayName { get; private set; }
        /// <summary>
        /// 描述信息
        /// </summary>
        public string Description { get; }

        private FieldDescriptor[] fields = null;
        /// <summary>
        /// 获取当前模型的所有字段描述
        /// </summary>
        /// <returns></returns>
        public FieldDescriptor[] GetFieldDescriptors()
        {
            if (fields == null)
            {
                var t = this.ModelType;
                var ps = t.GetProperties(BindingFlags.Public | BindingFlags.Instance);
                List<FieldDescriptor> list = new List<FieldDescriptor>();
                foreach (var item in ps)
                {
                    if (item.CanRead)
                    {
                        list.Add(new FieldDescriptor(item));
                    }
                }
                fields = list.ToArray();
            }
            return fields;
        }
        /// <summary>
        /// 获取模型定义的主键
        /// </summary>
        /// <returns></returns>
        public FieldDescriptor[] GetPrimaryKey()
        {
            var fs = GetFieldDescriptors();
            List<FieldDescriptor> list = new List<FieldDescriptor>();
            foreach (var item in fs)
            {
                if (item.IsPrimaryKey)
                {
                    list.Add(item);
                }
            }
            return list.ToArray();
        }

        /// <summary>
        /// 获取当前模型在数据库中使用的表名称
        /// </summary>
        /// <returns></returns>
        public string GetNameInDb()
        {
            if (!string.IsNullOrEmpty(MappingName))
            {//指定了映射名称
                return this.MappingName;
            }
            if (ModelType.IsSpecialName)
            {
                return ModelType.Name.TrimStart('@');//去掉特殊名称前面的“@”符号
            }
            return ModelType.Name;
        }

        /// <summary>
        /// 允许将<see cref="Type"/>隐式转换为<see cref="ModelDescriptor"/>
        /// </summary>
        /// <param name="type"></param>
        public static implicit operator ModelDescriptor(Type type)
        {
            return new ModelDescriptor(type);
        }
    }
}
